package com.lbd.gulimall.order.vo;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: linbd
 * @Description:
 * @Date: 2023/3/26 17:12
 */
public class OrderConfirmVO {
    @Getter
    @Setter
    private List<MemberReceiveAddressVO> memberReceiveAddress;
    @Getter
    @Setter
    private List<OrderItemVO> orderItems;
    @Getter
    @Setter
    private Integer integration;
    //订单总额
    private BigDecimal totalAmount = new BigDecimal("0");
    //应付价格
    private BigDecimal payAmount = new BigDecimal("0");;
    //商品总件数
    private Integer totalCount;
    //订单防重令牌（防止重复提交生成订单，幂等控制字段）
    @Getter
    @Setter
    private String orderToken;

    public Integer getTotalCount() {
        List<OrderItemVO> orderItems1 = getOrderItems();
        if (orderItems1 != null && orderItems1.size() > 0) {
            totalCount = Math.toIntExact(orderItems1.stream().map(OrderItemVO::getCount).reduce(0,(x, y) -> x + y));
        }
        return totalCount;
    }

    public BigDecimal getTotalAmount() {
        List<OrderItemVO> orderItems1 = getOrderItems();
        if (orderItems1 != null && orderItems1.size() > 0) {
            totalAmount = orderItems1.stream()
                    .map(OrderItemVO::getTotalPrice).reduce(new BigDecimal(0), (x, y) -> x.add(y));
        }
        return totalAmount;
    }

    public BigDecimal getPayAmount() {
        return getTotalAmount();
    }

    @Override
    public String toString() {
        return "OrderConfirmVO{" +
                "memberReceiveAddress=" + memberReceiveAddress +
                ", orderItems=" + orderItems +
                ", integration=" + integration +
                ", totalAmount=" + getTotalAmount() +
                ", payAmount=" + getPayAmount() +
                ", totalCount=" + getTotalCount() +
                ", orderToken='" + orderToken + '\'' +
                '}';
    }
}
